﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace Battle
{
    public class BoxLoad
    {
        public void LoadBoxCFG(string BotName, List<BattleBox> Boxes)
        {
            string filePath = BotName + "/config/Boxes.cfg";
            string line;
            int BoxIndex = 0;
            int Freqs = 2000;

            // Making sure it exists
            if (File.Exists(filePath))
            {
                StreamReader file = null;
                try
                {
                    file = new StreamReader(filePath);
                    // Check all lines
                    while ((line = file.ReadLine()) != null)
                    {
                        if (!line.StartsWith(";"))
                        {
                            if (line.StartsWith("[Box"))
                            {
                                Boxes.Add(new BattleBox());
                            }
                            else if (line.ToLower().StartsWith("name:"))
                            {
                                string[] info = SplitInfo(line);
                                Boxes[BoxIndex].BoxName = info[1];
                            }
                            else if (line.ToLower().StartsWith("location:"))
                            {
                                string[] info = SplitInfo(line);
                                ushort[] coord = new ushort[4];

                                for (int i = 1; i < 5; i++)
                                {
                                    coord[i - 1] = ushort.Parse(info[i]);
                                }
                                Boxes[BoxIndex].Location = coord;
                            }
                            else if (line.ToLower().StartsWith("lives:"))
                            {
                                Boxes[BoxIndex].Lives = int.Parse(SplitInfo(line)[1]);
                            }
                            else if (line.ToLower().StartsWith("gametype:"))
                            {
                                string gtype = SplitInfo(line)[1].ToLower().Trim();
                                if (gtype == GameType.Duel.ToString().ToLower())
                                    Boxes[BoxIndex].BoxGame = GameType.Duel;
                                else if (gtype == GameType.DZ2v2.ToString().ToLower())
                                    Boxes[BoxIndex].BoxGame = GameType.DZ2v2;
                                else if (gtype == GameType.Event.ToString().ToLower())
                                    Boxes[BoxIndex].BoxGame = GameType.Event;
                                else if (gtype == GameType.League.ToString().ToLower())
                                    Boxes[BoxIndex].BoxGame = GameType.League;
                                else
                                    Boxes[BoxIndex].BoxGame = GameType.Normal;
                            }
                            else if (line.ToLower().StartsWith("teams:"))
                            {
                                Boxes[BoxIndex].NumTeams = int.Parse(SplitInfo(line)[1]);
                            }
                            else if (line.ToLower().StartsWith("teamsize:"))
                            {
                                Boxes[BoxIndex].TeamSize = int.Parse(SplitInfo(line)[1]);

                                for (int i = 0; i < Boxes[BoxIndex].NumTeams; i++)
                                {
                                    BattleTeam newT = new BattleTeam();
                                    newT.Freq = (ushort)Freqs++;
                                    newT.TeamName = "Frequency [ " + newT.Freq + " ]";

                                    for (int j = 0; j < Boxes[BoxIndex].TeamSize; j++)
                                    {
                                        newT.TeamSlots.Add(new Slot());
                                    }

                                    Boxes[BoxIndex].Teams.Add(newT);
                                }

                                BoxIndex++;
                            }
                        }
                    }
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            }
        }
        private string[] SplitInfo(string message)
        {
            return message.Split(':');
        }
    }
}
